//
//  HMTabBarViewController.m
//  localLife
//
//  Created by Limonwei on 15/11/23.
//  Copyright © 2015年 heima. All rights reserved.
//

#import "HMTabBarViewController.h"
#import "HMFactory.h"

#define KTabTopViewHeight 44

@interface HMTabBarViewController ()

@property(nonatomic,strong)UIView *topView;

@property(nonatomic,strong)UIView *bottomView;

@end

@implementation HMTabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.tabBar.hidden = YES;
    
    
    _bottomView = [[UIView alloc]initWithFrame:CGRectMake(0, kUIScreenHeight-KTabTopViewHeight, kUIScreenWidth, KTabTopViewHeight)];
    
    _bottomView.backgroundColor = [UIColor blackColor];
    
    [self.view addSubview:_bottomView];
    
    
    for (int i = 0; i<5; i++) {
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        
        NSString *normalImage = [NSString stringWithFormat:@"home_%d",i];
        
        [button setBackgroundImage:[UIImage imageNamed:normalImage] forState:UIControlStateNormal];
        
        NSString *selectImage = [NSString stringWithFormat:@"home_%d_pressed",i];
        
        [button setBackgroundImage:[UIImage imageNamed:selectImage] forState:UIControlStateSelected];
        
        button.frame = CGRectMake(i*(kUIScreenWidth)/5.0, 0, kUIScreenWidth/5.0, KUITabBarHeight);
        
        button.tag = i;
        
        [button addTarget:self action:@selector(changeVC:) forControlEvents:UIControlEventTouchUpInside];
        
        [_bottomView addSubview:button];
        
        if (i == 0) {
            button.selected = YES;
        }
    }
    
    
    
    _topView = [[UIView alloc]initWithFrame:CGRectMake(0, kUIScreenHeight-KTabTopViewHeight-KUITabBarHeight, kUIScreenWidth, KTabTopViewHeight)];
    
    _topView.hidden = YES;
    
    [self.view addSubview:_topView];
    
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:_topView.bounds];
    
    imageView.image = [UIImage imageNamed:@"home_topbar"];
    
    [_topView addSubview:imageView];
    
    
    UIButton *Shake = [HMFactory type:0 withFrame:CGRectMake(0, 0, kUIScreenWidth*(123.0/640.0), KTabTopViewHeight) withTitle:@"摇一摇" withTarget:self withAction:@selector(changeVC:)];
    
    Shake.tag = 5;
    
    [_topView addSubview:Shake];
    
    UIButton *Contact = [HMFactory type:0 withFrame:CGRectMake(kUIScreenWidth * (150.0/640.0), 0, kUIScreenWidth * (180.0/640.0), KTabTopViewHeight) withTitle:@"联系商家" withTarget:self withAction:@selector(changeVC:)];
    
    Contact.tag = 6;
    
    [_topView addSubview:Contact];
    
    
    UIButton *Online = [HMFactory type:0 withFrame:CGRectMake(kUIScreenWidth * (350.0/640.0), 0, kUIScreenWidth * (108.0/640.0), KTabTopViewHeight) withTitle:@"在线" withTarget:self withAction:@selector(changeVC:)];
    
    Online.tag = 7;
    
    [_topView addSubview:Online];
    
    
    UIButton *US = [HMFactory type:0 withFrame:CGRectMake(kUIScreenWidth * ((640-150)/640.0), 0, kUIScreenWidth * (150.0/640.0), KTabTopViewHeight) withTitle:@"我们" withTarget:self withAction:@selector(changeVC:)];
    
    US.tag = 8;
    
    [_topView addSubview:US];

}



-(void)changeVC:(UIButton *)button{
    
    _topView.hidden = YES;
    
    [_bottomView.subviews enumerateObjectsUsingBlock:^(__kindof UIButton * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        //
        obj.selected = NO;
    }];
    
    button.selected = YES;
    
    if (button.tag<4) {
        self.selectedIndex = button.tag;
    }else if (button.tag == 4){
        _topView.hidden = NO;
    }else{
        
        self.selectedIndex = button.tag-1;
    }
    
    
}

-(void)showOrHiddenTabbarView:(BOOL)flag{
    
    if (flag) {
        _topView.hidden = YES;
        _bottomView.hidden = YES;
    }else{
        
        _bottomView.hidden = NO;
    }
    
}

@end

































